import React, { Component } from "react";
import { withRouter } from "react-router-dom";
import { Layout, Row, Col } from 'antd';
import { connect } from 'react-redux';
import { setCookie } from '@/utils';
import logo from "@/static/img/logo.png";
import user from '@/store/actions/user';

const { Header } = Layout;

class header extends Component{

    componentDidMount() {
        this.props.getUser()
    }

    render() {
        const { user = {} } = this.props;
        return (
            <Header>
                <Row>
                    <Col span={12}>
                        <a href="/">
                            <img src={logo} alt=""/>
                        </a>
                    </Col>
                    <Col span={12} className="user-info">
                        <span>欢迎你，{user.userName}</span>
                        <span className="logout" onClick={() => {this.logout()}}>退出</span>
                    </Col>
                </Row>
            </Header>
        )
    }

    // 退出登录
    logout (){
        setCookie('USER_TOKEN', '', -1);
        this.props.history.replace('/login');
    }
}


export default connect(state => state.user, user)(withRouter(header));